package com.gcxy.jk.shixunyi.service;

import cn.hutool.core.bean.BeanUtil;
import com.gcxy.jk.shixunyi.base.BaseService;
import com.gcxy.jk.shixunyi.dto.ScenicSpotInfoDto;
import com.gcxy.jk.shixunyi.dto.WxUserResponseDto;
import com.gcxy.jk.shixunyi.entity.ScenicSpot;
import com.gcxy.jk.shixunyi.entity.User;
import com.gcxy.jk.shixunyi.utils.ArithmeticUtils;
import com.gcxy.jk.shixunyi.utils.GetNowTime;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserService extends BaseService<User> {
    /**
     * 微信用户绑定
     * @param nickName
     * @param avatarUrl
     * @param gender
     * @param openid
     */
    public WxUserResponseDto bind(String nickName, String avatarUrl, Integer gender, String openid) {
        User user = new User();
        user.setOpenid(openid);

        // 通过openid去查询用户
        User us = this.mapper.selectOne(user);
        if(us == null) {
            // 没有就新增用户
            user.setNickName(nickName);
            user.setHeadPortrait(avatarUrl);
            user.setGender(Boolean.valueOf(gender.toString()));
            user.setCreateTime(GetNowTime.getNowTime());
            user.setCreateUser((user.getId()));
            this.mapper.insert(user);
        } else {
            // 有就修改用户
            user.setNickName(nickName);
            user.setHeadPortrait(avatarUrl);
            this.mapper.updateByPrimaryKey(user);
        }
        WxUserResponseDto wxUserResponseDto = BeanUtil.toBean(user, WxUserResponseDto.class);
        wxUserResponseDto.setAvatarUrl(user.getHeadPortrait());
        return wxUserResponseDto;
    }
    /**
     * 根据openid获取相关信息
     * @param openid
     * @return
     */
    public WxUserResponseDto getUserMessageByOpenid(String openid){
        Example example = Example.builder(User.class).build();
        example.createCriteria().andLike("openid", openid);
        User user = this.mapper.selectOneByExample(example);
        return BeanUtil.toBean(user, WxUserResponseDto.class);
    }

    /**
     *用户信息绑定
     * @param openid
     * @param prov
     * @param telephone
     */
    public void editMessage(String openid,String prov,String telephone,String nickName){
        //查询openid的用户
        Example example = new Example(User.class);
        example.createCriteria().andLike("openid", openid);
        //查询到这个用户
        User user = this.mapper.selectOneByExample(example);

        Example example2 = Example.builder(User.class).build();
        example2.createCriteria().andLike("openid", openid);
        user.setProv(prov);
        user.setPhone(telephone);
        //设置当前的时间存入数据库
        user.setNickName(nickName);
        user.setUpdateTime(GetNowTime.getNowTime());

        user.setUpdateUser(user.getId());
        this.mapper.updateByExampleSelective(user,example2);
    }
    /**
     * 绑定手机号
     * @param phone
     */
    public void insertPhone(String phone){
        User user = new User();
        User us = this.mapper.selectOne(user);
        String op = us.getOpenid();
        Example example = Example.builder(User.class).build();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(op).andAllEqualTo(phone);
        user.setPhone(phone);
        this.mapper.updateByExampleSelective(user,example);
    }

    /**
     * 用户城市属地更新
     * @param prov
     */
    public void insertProv(String prov){
        User user = new User();
        User user1 = this.mapper.selectOne(user);
        String op = user1.getOpenid();
        Example example = Example.builder(User.class).build();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(op).andAllEqualTo(prov);
        user.setProv(prov);//设置插入数据
        this.mapper.updateByExampleSelective(user,example);
    }

    /**
     *余额减法 用来提现
     * @param money
     * @return
     */
    public void subMoney(BigDecimal money,String openid){
        //查询openid的用户
        Example example = new Example(User.class);
        example.createCriteria().andLike("openid", openid);
        //查询到这个用户
        User user = this.mapper.selectOneByExample(example);
        BigDecimal userMoney = user.getMoney();
//        BigDecimal userMoney = user1.getMoney();
       // String openid = user1.getOpenid();
//
//        BigDecimal num1 = new BigDecimal(String.valueOf(userMoney));
//        BigDecimal num2 = new BigDecimal(String.valueOf(money));
//        //做一次运算 得到用户买东西后剩下的钱，或者提现之后剩下的
//        BigDecimal result2 = num1.subtract(num2);
//        Example example = Example.builder(User.class).build();
//        example.createCriteria().andLike("openid", openid);
//        User user = this.mapper.selectOneByExample(example);
//        return BeanUtil.toBean(user, WxUserResponseDto.class);
        //调用运算类进行减法运算
        BigDecimal result2 = BigDecimal.valueOf(ArithmeticUtils.sub(userMoney,money));

        Example example2 = Example.builder(User.class).build();
        example2.createCriteria().andLike("openid", openid);
//        criteria.andAllEqualTo(openid);
        user.setMoney(result2);//设置插入数据
        this.mapper.updateByExampleSelective(user,example2);
    }

    /**
     * 余额加 用来充值
     * @param money
     * @param openid
     */
    public void addMoney(BigDecimal money,String openid){
        //查询openid的用户
        Example example = new Example(User.class);
        example.createCriteria().andLike("openid", openid);
        //查询到这个用户
        User user = this.mapper.selectOneByExample(example);
        BigDecimal userMoney = user.getMoney();

        //调用运算类进行加法运算
        BigDecimal result2 = BigDecimal.valueOf(ArithmeticUtils.add(userMoney,money));
        Example example2 = Example.builder(User.class).build();
        example2.createCriteria().andLike("openid", openid);

        user.setMoney(result2);//设置插入数据
        this.mapper.updateByExampleSelective(user,example2);
    }
}
